package com.aaa.zs.filter;

import com.aaa.zs.service.SsoMemberService;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
 * fileName:SsoLoginFilter
 * description:
 * author: LJV
 * createTime:2020/12/7 11:33
 * version:1.0.0
 */
@Component
public class SsoLoginFilter extends ZuulFilter {

    @Autowired
    private SsoMemberService ssoMemberService;

    /*@Autowired
    private RedisTemplate redisTemplate;*/

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 15;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        System.out.println("进入了单点登录过滤器。。。。" );
//使用zuul中RequestContext提供获取上下文对象方法获取上下文对象
        RequestContext currentContext = RequestContext.getCurrentContext();
//获取HttpServletRequest
        HttpServletRequest request = currentContext.getRequest();
//获取HttpServletResponse
        HttpServletResponse response = currentContext.getResponse();
        //获取请求路径 /api/sso/memberLogin/authMember
        String requestURI = request.getRequestURI();
        //验证是否是登录请求
        /*if(requestURI.contains("memberLogin/authMember")){
            //放行
            currentContext.setSendZuulResponse(true);
        }*/
//        验证是否携带token  并且验证正确性
        String accessToken = request.getParameter("accessToken");
//        if(!StringUtils.isEmpty(accessToken)&&
//            ){
//
//        }
        if(requestURI.contains("userLogin/authUser")||
                requestURI.contains("memberLogin/authMember")||
                (!StringUtils.isEmpty(accessToken)&&
                        ssoMemberService.chechAccessToken(accessToken))){
            //放行
            currentContext.setSendZuulResponse(true);
            return null;
        }else if(!StringUtils.isEmpty(accessToken)&&
                !ssoMemberService.chechAccessToken(accessToken)){
            //不放行
            currentContext.setSendZuulResponse(false);
            //返回nologin
            try {
                response.setCharacterEncoding("GBK");
                response.getWriter().println("您的账号已在别处登陆");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            try {
//                //删除redis中的唯一识别
////                redisTemplate.delete(accessToken);
////                System.out.println("删除唯一识别");
                //不放行
                currentContext.setSendZuulResponse(false);
                //返回nologin
                response.getWriter().println("nologin");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


}
